package org.scalpel.mainApps;


import java.util.Timer;
import java.util.TimerTask;

import org.scalpel.mainApps.R;
import org.scalpel.mainApps.HHObjects.ModLoco;
import org.scalpel.mainApps.IoioMotorControler;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Canvas;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
import android.widget.ToggleButton;

/**
 * 
 * @author Sylvere Billout
 * 
 * Main Activity
 * 
 * 
 **/
public class ScalpelActivity extends Activity {
	//IoioMotorControler imc=new IoioMotorControler();
	Canvas fCanvas;
	RobotView fRobotView;
	boolean fSimulation;
		
	private Timer fTimer;
	
	public  ToggleButton button_;
	public  boolean Start_Droite;
	public static final String PREFS_NAME = "scalpel";

	public static final int PREFERENCE_ACTION = 1002;


	@Override
	public void onPause(){
		super.onPause();
		// Kill the apps :
		finish();
	}
	
	@Override
	public void finish(){
		super.finish();
		fRobotView.finish();
	}
	
	/** 
	 * Call when activity is create at the first time
	 */
	@Override
	public void onCreate(Bundle icicle) {
		
	  super.onCreate(icicle); 
	  setContentView(R.layout.main);
	  
	  fRobotView = (RobotView)this.findViewById(R.id.robotView);
		//button_ = (ToggleButton) findViewById(R.id.toggleButton1);
		
		  /*button_.setOnClickListener(new View.OnClickListener() {
	             public void onClick(View v) {
					  if(button_.isPressed())
	    			  {
						  //imc.update();
						  // modifier  <constant name="startposx" class="double" value="0.25"/>
						 // <constant name="startposy" class="double" value="0.25"/>
						  //<constant name="startposa" class="double" value="0"/>
						  //Worlmap
						  //Faire un Parse...
	    				 Start_Droite = false;
	    			  }
					  else
					  {
		    			 Start_Droite = true;
					  }
	             }
		  });*/

		
		
	  fRobotView.setOnTouchListener(
      new View.OnTouchListener() {
      	@Override
      	public boolean onTouch(View myView, MotionEvent event) {
      		int action = event.getAction();
      		if (action == MotionEvent.ACTION_MOVE){
      			fRobotView.onTouch(event);
      		}
    			if (action == MotionEvent.ACTION_UP){
    				fRobotView.onTouch(event);
    			}
    			
//    			if (action == MotionEvent.ACTION_DOWN){
//    				fRobotView.onTouch(event);
//    			}
    			return true;
    		}
    	}
    );
	  

	}

	@Override
	protected void onStart(){
		super.onStart();
		fTimer = new Timer();
	  fTimer.schedule(new UpdateDrawTask(), 30, 60);


	}
	
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
  	switch (item.getItemId()) {
      case R.id.Menu_simulation:
    		SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
    		SharedPreferences.Editor editor = settings.edit();
    		boolean isSimulation = settings.getBoolean("simulation", true);
    		editor.putBoolean("simulation", !isSimulation);
        // Commit the edits!
        editor.commit();
        return true;
      case R.id.Menu_preferences:
      	Intent intent = new Intent().setClass(this, PrefActivity.class);    
      	Log.i("preferences","in settings, created intent");
        startActivityForResult(intent, PREFERENCE_ACTION);
        Log.i("preferences","in settings, started activity for result");
        return true;
		
		}

    return false;
  }
	
 	@Override
 	protected void onResume() {
 	  super.onResume();
 	}

 	@Override
 	protected void onStop() {
 	  super.onStop();
 	}
 	
 	@Override
 	protected void onDestroy(){
 		super.onDestroy();
 	}
 	
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      MenuInflater inflater = getMenuInflater();
      inflater.inflate(R.menu.option_menu, menu);
      return true;
  }
  
  
 	
 	
 	class UpdateDrawTask extends TimerTask {
    public void run() {
			fRobotView.postInvalidate(); // force view to be updated (call onDraw)
    }
  }
}